//
//  CoueousPic.m
//  Slice
//
//  Created by lanou on 16/6/17.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "CoueousPic.h"
@interface CoueousPic ()<UIScrollViewDelegate>
//底层的scrollView
@property (nonatomic, strong)UIScrollView *scroller;

//位置指示器
@property (nonatomic, strong)UIPageControl *pageControl;

//定时器
@property (nonatomic, strong) NSTimer  *timer;

@end




@implementation CoueousPic

//两个必要的参数
- (instancetype)initWithFrame:(CGRect)frame images:(NSArray *)images {
      self = [super initWithFrame:frame];
    if (self) {
        if (images.count == 0) {
            return nil;
        }
        CGFloat kw = frame.size.width;
        CGFloat kh = frame.size.height;
        //        初始化scrollView
        self.scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kw, kh)];
        self.scroller.showsHorizontalScrollIndicator = NO;
        self.scroller.bounces = NO;
        self.scroller.pagingEnabled = YES;
        if (images.count == 1) {
             self.scroller.contentOffset = CGPointMake(0, 0);
        }
        if (images.count > 1) {
            self.scroller.contentOffset = CGPointMake(kw, 0);
            self.scroller.delegate = self;

            //        初始化page
            self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kh - 20, kw, 20)];
            self.pageControl.numberOfPages = images.count;
            [self addSubview:self.scroller];
            [self addSubview:self.pageControl];
            //        创建定时器
            self.timer = [NSTimer scheduledTimerWithTimeInterval:intreVal target:self selector:@selector(time1) userInfo:nil repeats:YES];
            //        添加动作
            [self.pageControl addTarget:self action:@selector(change ) forControlEvents:UIControlEventValueChanged];
        }
//        重新配置scrollView
        [self setupScrollView:images];
    }
    return self;
}





//重新配置轮播图数据
- (void)setupScrollView:(NSArray *)array {
    if (array.count == 1) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClick:)];
        [imgView addGestureRecognizer:tap];
        imgView.userInteractionEnabled = YES;
        [self.timer setFireDate:[NSDate distantFuture]];
        [imgView sd_setImageWithURL:array.lastObject];
        [self addSubview:imgView];
        return;
    }
     
    
//    设置滚动区域
    self.scroller.contentSize = CGSizeMake((array.count + 2)* self.frame.size.width, 0);
    for (int i = 0; i < array.count + 2; i ++) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClick:)];
        [imgView addGestureRecognizer:tap];
        imgView.userInteractionEnabled = YES;
        
//        第一张加图显示真实的最后一张
        if ( i == 0) {
            [imgView sd_setImageWithURL:[NSURL URLWithString:array.lastObject]];
        } else if (i == array.count + 1) {
            [imgView sd_setImageWithURL:[NSURL URLWithString:array.firstObject]];
        } else {
            [imgView sd_setImageWithURL:[NSURL URLWithString:array[i - 1]]];
        }
    [self.scroller addSubview:imgView];
        
    }
}

//定时器的方法
- (void)time1 {
    
    NSInteger index = self.scroller.contentOffset.x / self.frame.size.width + 1;

//    挡结构体和对象连用时不能直接复制
    CGPoint offset = self.scroller.contentOffset;
    
    offset.x = self.frame.size.width * index;
    
//    改变偏移量
    self.scroller.contentOffset = CGPointMake( offset.x, 0);
    
}

//pageControl的定做方法
- (void)change {
//    关闭定时器
    [self.timer setFireDate:[NSDate distantFuture]];
    [UIView animateWithDuration:0.3 animations:^{
        self.scroller.contentOffset = CGPointMake((self.pageControl.currentPage + 1) * self.frame.size.width, 0);

    } completion:^(BOOL finished) {
//        开启定时器
        [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:intreVal]];
    }];
}

//点击图片手势的方法
- (void)imageClick:(UITapGestureRecognizer *)tap {
    NSInteger index = self.scroller.contentOffset.x / self.frame.size.width;
    
    if (self.imgClickBlock) {
        
        if (self.scroller.contentSize.width == self.frame.size.width) {
            self.imgClickBlock(index);
        } else {
            self.imgClickBlock(index);
        }
    }
}

#pragma mark ------代理方法-------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    滚动到最前面 回到真是的最后一张
    if (scrollView.contentOffset.x <= 0) {
        self.scroller.contentOffset = CGPointMake(self.frame.size.width * self.pageControl.numberOfPages, 0);
    } else if (scrollView.contentOffset.x >= (self.pageControl.numberOfPages + 1) * self.frame.size.width) {
        self.scroller.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    
    self.pageControl.currentPage = scrollView.contentOffset.x / self.frame.size.width - 1;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//    定时器暂停 没有暂停方法 只能设置 开启时间 在遥远的未来
    [self.timer setFireDate:[NSDate distantFuture]];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:intreVal]];
}




@end
